package com.atguigu.dga.governance.assessor;

import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Component("TABLE_NAME_STANDARD")
public class TableNameStandardAssessor extends Assessor {

    //^ods_\w+_(inc|full)$
    Pattern odsPattern= Pattern.compile("^ods_\\w+_(inc|full)$");
    Pattern dimPattern= Pattern.compile("^dim_\\w+_(zip|full)$");
    Pattern dwdPattern= Pattern.compile("^dwd_\\w+_\\w+_(inc|full)$");
    Pattern dwsPattern= Pattern.compile("^dws_\\w+_\\w+_\\w+_(1d|nd|td)$");
    Pattern adsPattern= Pattern.compile("^ads_\\w+$");
    Pattern dmPattern= Pattern.compile("^dm_\\w+$");


    @Override
    public void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException {
        String dwLevel = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getDwLevel();
        if(dwLevel.equals("OTHER")){
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(5));
            governanceAssessDetail.setAssessProblem("未纳入分层");
            return;
        }
        Matcher matcher = null;
        String tableName = assessParam.getTableMetaInfo().getTableName();
        if (dwLevel.equals("ODS")) {
            matcher= odsPattern.matcher(tableName);
        } else if (dwLevel.equals("DIM")) {
            matcher= dimPattern.matcher(tableName);
        } else if (dwLevel.equals("DWD")) {
            matcher= dwdPattern.matcher(tableName);
        } else if (dwLevel.equals("DWS")) {
            matcher= dwsPattern.matcher(tableName);
        } else if (dwLevel.equals("ADS")) {
            matcher= adsPattern.matcher(tableName);
        } else if (dwLevel.equals("DM")) {
            matcher= dmPattern.matcher(tableName);
        }

        if(!matcher.matches()){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("表名不符合规范");
        }
    }


    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("^\\w{2,12}@\\w{1,10}\\.(com|org|cn)$");
        String  testEmail="zhangchen@***atguigu.com";
        Matcher matcher = pattern.matcher(testEmail);
        if(matcher.matches()){
            System.out.println(" 符合规范！ " );
        }else{
            System.out.println(" 不符合规范！ " );
        }


    }
}
